// 我们不一定只用localStorage，有可能使用sessionStorage
// 枚举两个类型，判断当前使用的是localStorage还是sessionStorage
enum StorageType {
  Local,
  Session
}

class MyStorage {
  storage: Storage

  constructor(type: StorageType) {
    this.storage = type === StorageType.Local ? localStorage : sessionStorage
  }

  setStorage(key: string, val: any) {
    if (val) {
      this.storage.setItem(key, JSON.stringify(val))
    }
  }

  getStorage(key: string) {
    const val = this.storage.getItem(key)

    if (val) {
      return JSON.parse(val)
    }
  }

  removeStorage(key: string) {
    this.storage.removeItem(key)
  }

  clearAllStorage() {
    this.storage.clear()
  }
}

const localCache = new MyStorage(StorageType.Local)
const sessionCache = new MyStorage(StorageType.Session)

export { localCache, sessionCache }
